<?php
defined('ANT_ACCESS') or die('Access denided');
/**
 * Ant Framework
 *
 * An open source application development framework for PHP 5
 *
 * @package		Ant Framework
 * @author		Meshin Dmitriy
 * @copyright	Copyright (c) 2008, Meshin Dmitriy
 * @license		GNU Public Lisence v2
 * @link		http://code.google.com/p/ant-framework
 * @desc		Main controller class
 **/
	class controller{
		protected $baseclasses = array('benchmark', 'loader', 'view', 'headers');
		public $configs = array();
		protected $objects;
		
		function __set($name, $value){
			if(!isset($this->objects[$name])){
				$this->objects[$name] = &$value; 
			}
		}
		
		function __get($name){
			return (isset($this->objects[$name]) ? $this->objects[$name] : NULL);
		}

		function __construct(){
			
			//	базовые конфиги
			$this->configs['base'] = get_config('configs');
			
			//	подключение хуков
			if(TRUE === $this->configs['base']['enable_hooks']){
				$this->hooks = load_class('hooks');
				$this->hooks->hook('pre_construct');
			}
			
			//	базовые классы
			foreach($this->baseclasses as $class){
				$this->$class = load_class($class);
			}

			//	заголовки
			$this->headers->set_header('Content-type: '.
					$this->configs['base']['content-type'].'; charset='.
					$this->configs['base']['charset']
			);
			
			if(TRUE === $this->configs['base']['enable_hooks']){
				$this->hooks->hook('pre_autoload');
			}
		}
		
		public function autoload($autoload){
			//	автозагрузка конфигов
			foreach($autoload['configs'] as $config){
				$this->configs[$config] =& get_config($config);
			}
			
			//	автозагрузка системных библиотек
			foreach($autoload['sys_libs'] as $lib){
				$this->loader->library($lib);
			}
			
			//	автозагрузка пользовательских библиотек
			foreach($autoload['libs'] as $lib){
				$this->loader->library($lib, FALSE);
			}
			
			//	автозагрузка остального
			$included_array = array('configs', 'sys_libs', 'libs');
			foreach($autoload as $type => $items){
				foreach($items as $item){
					if(!in_array($type, $included_array)){
						require_once(get_folder($type).$item.EXT);
					}
				}
			}
		}

		//	запуск приложения на выполнение
		public function ApplicationRun(){
			
			if(TRUE === $this->configs['base']['enable_hooks']){
				$this->hooks->hook('pre_headers');
			}
			
			//	отправляем заголовки
			$this->headers->send();
						
			if(TRUE === $this->configs['base']['enable_hooks']){
				$this->hooks->hook('pre_render');
			}
			
			//	запускаем шаблоны
			$this->view->render();
			
			if(TRUE === $this->configs['base']['enable_hooks']){
				$this->hooks->hook('post_render');
			}
		}
	}
?>